﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using Assist;
using YJKSparseDistance.ModelLayer;
using YJKSparseDistance.Properties;
using YArchitech.LIB;

namespace YJKSparseDistance.ViewLayer
{
	public partial class BuildingTypeSelectForm : System.Windows.Forms.Form
	{
		public RulePacker ReturnValue
		{
			get
			{
				return this.m_ReturnValue;
			}
		}

		public XmlDocument UseDocument
		{
			get
			{
				return this.m_UseDocument;
			}
			private set
			{
				this.m_UseDocument = value;
			}
		}

		private BuildingTypeSelectForm()
		{
			this.InitializeComponent();
			this.readXml();
			this.PrepareBuildingType();
			this.comboBox_BuildingType.SelectedIndexChanged += this.Event_BuildingTypeChange;
		}

		public BuildingTypeSelectForm(string xmlPath)
		{
			this.InitializeComponent();
			this.m_sUsePath = xmlPath;
			this.readXml();
			this.PrepareBuildingType();
			this.PrepareFireLevel();
			if (this.UseDocument != null)
			{
				this.comboBox_BuildingType.SelectedIndexChanged += this.Event_BuildingTypeChange;
			}
		}

		private void BuildingTypeSelectForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.comboBox_BuildingType.SelectedIndex = this.m_nlastSelectBuildingTypeIndex;
			this.comboBox_FireLevel.SelectedIndex = this.m_nlasSecectFireLevelIndex;
		}

		private void comboBox_BuildingType_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void readXml()
		{
			this.TryReadXml();
			if (this.UseDocument != null)
			{
				foreach (object obj in this.UseDocument.DocumentElement.ChildNodes)
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode is XmlElement && (xmlNode as XmlElement).Name.Equals(YJKSparseDistance.Properties.Resources.Str_NodeName_BuildingType))
					{
						Dictionary<string, RulePacker> dictionary = new Dictionary<string, RulePacker>();
						this.m_listAllRules.Add((xmlNode as XmlElement).GetAttribute(YJKSparseDistance.Properties.Resources.Str_Attribute_Name), dictionary);
						this.AddLevelValue(dictionary, xmlNode);
					}
					else if (xmlNode is XmlElement && (xmlNode as XmlElement).Name.Equals(YJKSparseDistance.Properties.Resources.Str_NodeName_Select))
					{
						this.m_nlastSelectBuildingTypeIndex = int.Parse((xmlNode as XmlElement).GetAttribute(YJKSparseDistance.Properties.Resources.Str_NodeName_BuildingType));
						this.m_nlasSecectFireLevelIndex = int.Parse((xmlNode as XmlElement).GetAttribute(YJKSparseDistance.Properties.Resources.Str_NodeName_LevelType));
					}
				}
			}
		}

		public bool TryReadXml()
		{
			this.UseDocument = new XmlDocument();
			string text = this.m_sUsePath + YJKSparseDistance.Properties.Resources.Str_XmlFileName;
			if (File.Exists(text))
			{
				this.UseDocument.Load(text);
			}
			else if (!File.Exists(text) && File.Exists(YJKSparseDistance.Properties.Resources.Str_DefultXmlPath + YJKSparseDistance.Properties.Resources.Str_XmlFileName))
			{
				this.UseDocument.Load(YJKSparseDistance.Properties.Resources.Str_DefultXmlPath + YJKSparseDistance.Properties.Resources.Str_XmlFileName);
			}
			else
			{
				this.UseDocument = null;
			}
			return this.UseDocument != null;
		}

		private void AddLevelValue(Dictionary<string, RulePacker> tempDic, XmlNode tempChilNodeO)
		{
			foreach (object obj in (tempChilNodeO as XmlElement).ChildNodes)
			{
				if (obj is XmlElement && (obj as XmlElement).Name.Equals(YJKSparseDistance.Properties.Resources.Str_NodeName_LevelType))
				{
					tempDic.Add((obj as XmlElement).GetAttribute(YJKSparseDistance.Properties.Resources.Str_Attribute_Name), this.UesNodeMakeRule(obj as XmlNode));
				}
			}
		}

		private RulePacker UesNodeMakeRule(XmlNode tempChilNodeO)
		{
			XmlElement xmlElement = (tempChilNodeO as XmlElement).FirstChild as XmlElement;
			if (xmlElement != null)
			{
				return new RulePacker((tempChilNodeO as XmlElement).GetAttribute(YJKSparseDistance.Properties.Resources.Str_Attribute_Name), (tempChilNodeO as XmlElement).GetAttribute(YJKSparseDistance.Properties.Resources.Str_Attribute_Name), AssistFunc.mmToFeet(double.Parse(xmlElement.GetAttribute(YJKSparseDistance.Properties.Resources.Str_Attribute_NoneEnd))) * double.Parse(YJKSparseDistance.Properties.Resources.Str_OneK), AssistFunc.mmToFeet(double.Parse(xmlElement.GetAttribute(YJKSparseDistance.Properties.Resources.Str_Attribute_End))) * double.Parse(YJKSparseDistance.Properties.Resources.Str_OneK));
			}
			return null;
		}

		private void PrepareBuildingType()
		{
			if (this.UseDocument != null)
			{
				this.comboBox_BuildingType.Items.Clear();
				object[] items = (from k in this.m_listAllRules
				select k.Key).ToArray<string>();
				this.comboBox_BuildingType.Items.AddRange(items);
				this.comboBox_BuildingType.SelectedIndex = 0;
			}
		}

		private void Event_BuildingTypeChange(object sender, EventArgs e)
		{
			this.PrepareFireLevel();
		}

		private void PrepareFireLevel()
		{
			if (this.UseDocument != null)
			{
				this.comboBox_FireLevel.Items.Clear();
				string text = this.comboBox_BuildingType.SelectedItem as string;
				if (!string.IsNullOrEmpty(text) && !string.IsNullOrWhiteSpace(text))
				{
					object[] items = (from k in this.m_listAllRules[text]
					select k.Key).ToArray<string>();
					this.comboBox_FireLevel.Items.AddRange(items);
					this.comboBox_FireLevel.SelectedIndex = 0;
				}
			}
		}

		private void PrepareReturnValue()
		{
			if (this.UseDocument != null)
			{
				this.m_ReturnValue = this.m_listAllRules[this.comboBox_BuildingType.SelectedItem as string][this.comboBox_FireLevel.SelectedItem as string];
			}
		}

		private void btn_Ok_Click(object sender, EventArgs e)
		{
			this.PrepareReturnValue();
			base.Close();
		}

		private void BuildingTypeSelectForm_FormClosed(object sender, FormClosedEventArgs e)
		{
		}

		private void SaveXml()
		{
			if (this.m_UseDocument != null)
			{
				foreach (object obj in this.UseDocument.DocumentElement.ChildNodes)
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode is XmlElement && (xmlNode as XmlElement).Name.Equals(YJKSparseDistance.Properties.Resources.Str_NodeName_Select))
					{
						(xmlNode as XmlElement).SetAttribute(YJKSparseDistance.Properties.Resources.Str_NodeName_BuildingType, this.comboBox_BuildingType.SelectedIndex.ToString());
						(xmlNode as XmlElement).SetAttribute(YJKSparseDistance.Properties.Resources.Str_NodeName_LevelType, this.comboBox_FireLevel.SelectedIndex.ToString());
					}
				}
				this.m_UseDocument.Save(this.m_sUsePath + YJKSparseDistance.Properties.Resources.Str_XmlFileName);
			}
		}

		private void btn_Cancle_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private XmlDocument m_UseDocument;

		private RulePacker m_ReturnValue;

		private string m_sUsePath = string.Empty;

		private int m_nlastSelectBuildingTypeIndex;

		private int m_nlasSecectFireLevelIndex;

		private Dictionary<string, Dictionary<string, RulePacker>> m_listAllRules = new Dictionary<string, Dictionary<string, RulePacker>>();
	}
}
